package TUILayer;
import java.util.Scanner;
import ControllerLayer.*;

public class OrderUI
{   

    private OrderController orderController;
    private String idAssistant;
    
    public OrderUI(String idAssistant)
    {
        this.idAssistant = idAssistant;
        orderController= orderController.getInstance();
    }
    
    public void start()
    {
        orderMenu();
    }
    
    public void setIDAssistant(String id)
    {
        idAssistant=id;
    }
    
    public String getIDAssistant()
    {
        return idAssistant;
    }
    
    private void orderMenu()
    {
        int idOrder = 0;
       boolean exit = false;
       while(!exit)
       {
          int choise = writeOrderMenu(); 
          if(choise == 1)
          {  
              makeOrderLine();
          }
          else{
              if(choise == 2){
                   idOrder = createOrder();
              }  
              else{
                   exit = true;
                        }
                    }
                }
            
       }
  
    private int writeOrderMenu()
    {      
            Scanner keyboard = new Scanner(System.in);
            System.out.println("\f *** Order Menu ***");
            System.out.println(" Logged as: " +getIDAssistant());
            System.out.println(" (1) Make shopping");
            System.out.println(" (2) Create order");
            System.out.println(" (3) Return");
            System.out.print("\n Make your choice: ");
            int choise = keyboard.nextInt();
            return choise;
        }
        
  
    private String  inputBarCode()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Input item code: ");
        String address = keyboard.nextLine();
        return address;
    }
    
    
      private int inputNumberOfProducts()
    {   
        boolean ok = false;
        int id=0;
        while(!ok)
        {
           Scanner keyboard = new Scanner(System.in);
           System.out.println(" How many products? :   ");
           try{
             id = keyboard.nextInt();
             ok = true;
           }
           catch (Exception e)
            {
             System.out.println("It should be a number -  try again");
             String input = keyboard.nextLine();
           }
        }
        return id;
    }
    
     private int inputID()
    {   
        boolean ok = false;
        int id=0;
        while(!ok)
        {
           Scanner keyboard = new Scanner(System.in);
           System.out.println(" Order ID :   ");
           try{
             id = keyboard.nextInt();
             ok = true;
           }
           catch (Exception e)
            {
             System.out.println("It should be a number -  try again");
             String input = keyboard.nextLine();
             inputContinue();
           }
        }
        return id;
    }
    
     private int inputIDCustomer()
    {   
        boolean ok = false;
        int id=0;
        while(!ok)
        {
           Scanner keyboard = new Scanner(System.in);
           System.out.println(" Customer ID :   ");
           try{
             id = keyboard.nextInt();
             ok = true;
           }
           catch (Exception e)
            {
             System.out.println("It should be a number -  try again");
             String input = keyboard.nextLine();
             inputContinue();
             
           }
        }
        return id;
    }
    
    private void makeOrderLine()
    {
        orderController.makeNewList();
        boolean ok=false;
        while(!ok)
        {
            String barCode=inputBarCode();
            if(!barCode.equals(""))
            {
               try{
                    if(orderController.searchItem(barCode)!=null){
                        int nr=inputNumberOfProducts();                
                        if(orderController.makeOrderLine(barCode, nr)){}
                        else{
                            System.out.println("There are not enough products for the order.");
                        }
                }
              }
              catch(NullPointerException ex){
                    System.out.println("There's no such product code: "+barCode+'\n'+"Please re-enter product code.");
                    
              }
            }
            else {
                System.out.println("Please re-enter product code.");
                ok=true;
                
            }
        }
        inputContinue();
    }
    
    
    private int createOrder()
    {
        orderController.raiseID();
        int idCustomer=inputIDCustomer();
        String idAssistant= getIDAssistant();
        orderController.makeSale(idCustomer, idAssistant);
        confirmOrder(orderController.getID());
        return orderController.getID();
    }
        
        
    private void confirmOrder(int idOrder)
    {
        boolean ok=false;
        while(!ok)  {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Confirm order?(y/n)?:  ");
        String address = keyboard.nextLine();
        if(address.equals("y")||address.equals("Y"))
        {
            System.out.println("The order is confirmed"+'\n'+ "Printing the invoice...");
            System.out.println(orderController.printInvoice(idOrder));
            inputContinue();
            ok=true;
        }else
        if(address.equals("n")||address.equals("N"))
        {
            orderController.deleteOrder(idOrder);
            System.out.println("The order has been deleted");
            inputContinue();
            ok=true;
        }
        else
        {
            System.out.println("Answer incorrect, try again");
        }
    }
    }
    
    private void inputContinue()
    {
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Press Enter to continue:  ");
         String code = keyboard.nextLine();
    }  
}